// pages/goods-more-lists/index.js
import {
    getNavigationbar,
} from "../../api/index.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        navigateName: "",
        scrollViewHeight: 0,numGood: 0,
        isSpecs: false,
        goodsList: [],
        obj:{},
        districtInfo: null,
        cartGoodDetail: {}, //购物车商品数据
        params: {},
        isAhow: false,
        goodListP: 1,

    },
    nood() {
        console.log('点击', this.data.subdata)
        if (this.data.obj.shop_status == 0 || this.data.obj.shop_status == 1) {
            this.setData({
                isAhow: true,
            })
            return
        }
        wx.reLaunch({
            url: `/pages/goods_cart/index`,
        })

    },
    getShoppingCartList(item) {
        getNavigationbar({
            act: 'getcartsp',

        }).then(res => {
            console.log('购物车列表-一件代发', res)
            this.setData({
                numGood: res.numbers
            })

        });
    },
    /**
     * 根据路由跳转名称获取对应商品列表
     * @param {*} name 
     */
    getGoodsList(params) {
        const {
            name,
            value
        } = params
        switch (name) {
            case '单品促销':
                this.getHttpGoodsList({
                    act: 'unique_product_list',
                    lack_inventory: 1,
                    p: this.data.goodListP
                })
                break;
            case '主粮':
                this.getHttpGoodsList({
                    act: 'district_detail',
                    lack_inventory: 1,
                    district_id: value.district_id,
                    p: this.data.goodListP
                })
                break;
            case '零食':
                this.getHttpGoodsList({
                    act: 'district_detail',
                    lack_inventory: 1,
                    district_id: value.district_id,
                    p: this.data.goodListP
                })
                break;
            case '用品':
                this.getHttpGoodsList({
                    act: 'district_detail',
                    lack_inventory: 1,
                    district_id: value.district_id,
                    p: this.data.goodListP
                })
                break;
            case '营养保健':
                this.getHttpGoodsList({
                    act: 'district_detail',
                    lack_inventory: 1,
                    district_id: value.district_id,
                    p: this.data.goodListP
                })
                break;
            case '9.9':
                this.getHttpGoodsList({
                    act: 'district_detail',
                    lack_inventory: 1,
                    district_id: value.activeid,
                    p: this.data.goodListP
                })
                break;
            case '9.6':
                this.getHttpGoodsList({
                    act: 'district_detail',
                    lack_inventory: 1,
                    district_id: value.activeid,
                    p: this.data.goodListP
                })
                break;
            case '药品专区':
                this.getHttpGoodsList({
                    act: 'district_detail',
                    lack_inventory: 1,
                    district_id: value.activeid,
                    p: this.data.goodListP
                })
                break;
            case name:
                this.getHttpGoodsList({
                    act: 'district_detail',
                    lack_inventory: 1,
                    district_id: value.activeid || value.district_id,
                    p: this.data.goodListP
                })
                break;
        }
    },
    /**
     * 执行请求获取数据回调方法数据
     */
    getHttpGoodsList(params) {
        console.log(params)
        getNavigationbar(params).then(res => {
            if (res) {
                console.log('getHttpGoodsList',res)
               
                switch (params.act) {
                    case 'unique_product_list':
                        res.forEach(element => {
                            if(this.data.obj.shop_status == 0 ||this.data.obj.shop_status == 1 ) {
                                element.sellprice = '***'
                                element.marketprice = '***'
                            } 
                        });
                        wx.nextTick(() => {
                            this.setData({
                                goodsList: this.data.goodsList.concat(res),
                                districtInfo: null
                            })
                        })
                        break;
                    case 'district_detail':
                        res.goods.forEach(element => {
                            if(this.data.obj.shop_status == 0 ||this.data.obj.shop_status == 1 ) {
                                element.sellprice = '***'
                                element.marketprice = '***'
                            } 
                        });
                        wx.nextTick(() => {
                            res.goods.activity = res.goods.activity_list
                            res.goods.forEach(item => {
                                item.activity = item.activity_list
                            })
                            console.log("res", res)
                            this.setData({
                                goodsList: this.data.goodsList.concat(res.goods),
                                districtInfo: {
                                    name: res.name,
                                    state: res.state,
                                    style: res.style,
                                    thumb: res.thumb,
                                },
                                navigateName: res.name
                            })
                        })
                        break;
                }
                this.computeScrollViewHeight() // 重新执行页面计算
            }
        })
    },

    /**
     * 跳转商品详情页回调函数方法
     */
    handleGoodsDetail(e) {
        console.log("跳转详情", e)
        const {
            value
        } = e.currentTarget.dataset
        const params = {
            goodsId: "",
            area: "",
            detailType: 1,
            act: "good_detail"
        }
        if (value) {
            const {
                goodsid,
                id
            } = e.currentTarget.dataset.value
            if (goodsid) {
                params.goodsId = goodsid
            } else {
                params.goodsId = id
            }
        } else {
            const {
                id
            } = e.detail
            params.goodsId = id
        }
        console.log("params", params)
        wx.navigateTo({
            url: `/pages/goods_detail/index?data=${JSON.stringify(params)}`,
        })
    },

    /**
     * 计算页面可滚动区域的动态高度
     */
    computeScrollViewHeight() {
        let that = this
        let query = wx.createSelectorQuery().in(this)
        query.select('.header').boundingClientRect()
        query.exec(res => {
            let headerHeight = res[0].height;
            let winHeight; // 窗口可使用高度
            wx.getSystemInfo({
                success: (res) => {
                    winHeight = res.windowHeight
                }
            })
            let scrollHeight = winHeight - headerHeight - 40; // 滚动区域高度
            that.setData({
                scrollViewHeight: scrollHeight
            })
        })
    },
    /**
     * 选择商品规格加入购物车回调函数方法
     */
    handleJoinCart(e) {
        const {
            value
        } = e.currentTarget.dataset
        console.log("value", value)
        if (this.data.obj.shop_status == 0 || this.data.obj.shop_status == 1) {
            this.setData({
                isAhow: true,
            })
         return
        }
        getNavigationbar({
            act: 'good_detail',
            id: value.id
        }).then(res => {
            this.setData({
                isSpecs: true,
                cartGoodDetail: res
            })
        })

        console.log(this.data.cartGoodDetail)
    },
    /**
     * 完成规格选择将商品添加至购物车
     */
    handleConfirm(e) {
        let data = e.detail

        console.log("data", data)
        // if (data.type == 1) {
        getNavigationbar({
            act: 'cart',
            id: data.commodityId || '',
            propertyid: data.data.propertyid || '',
            numbers: data.count || '',
        }).then(res => {
            console.log('添加购物车', res)
            if (res.result == 101) {
                wx.nextTick(() => {
                    getApp().Tips({
                        title: res.msg
                    })
                })
            } else {
                this.setData({
                    isSpecs: false,
                    cartGoodDetail: {}
                })
                wx.nextTick(() => {
                    getApp().Tips({
                        title: "商品已添加至购物车"
                    })
                })
            }
            this.getShoppingCartList()
        });
    },
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        if (options.data) {
            let params = JSON.parse(options.data)
            this.setData({
                navigateName: params.name,
                params: params
            })
            console.log('进来', params)
            wx.nextTick(() => {
                this.getGoodsList({
                    name: params.name,
                    value: params.value,
                    goodListP: this.data.goodListP
                })
            })
        }
        let res = wx.getStorageSync('USER')
        console.log('setStorageSync',res)
        this.data.obj = res
        this.getShoppingCartList()
    },

    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        this.computeScrollViewHeight()
    },

    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {

    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    },

    scrollToLower() {
        this.data.goodListP++
        wx.nextTick(() => {
            this.getGoodsList({
                name: this.data.navigateName,
                value: this.data.params.value,
                p: this.data.goodListP
            })
        })
      },
})